package com.lpdemilis.pokermanager;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity implements OnClickListener,
		MyAsyncListener {
	protected EditText userText;
	protected EditText passwordText;
	protected CheckBox rememberCheckBox;
	protected Button accessButton;
	protected TextView returnTextView;
	protected User user;
	protected String SERVER_URL;

	public static final String PREFS_NAME = "MyPrefsFile"; // unique identifier
															// for our
															// Preferences
	private static final String PREF_USERNAME = "username"; // fields to be
															// saved
	private static final String PREF_PASSWORD = "password";
	private static final String PREF_CHECKED = "checked";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		if (Build.MANUFACTURER.equals("unknown")){
			SERVER_URL = this.getString(R.string.URL_local);
		}else{
			SERVER_URL = this.getString(R.string.URL);
		}

		// interface
		userText = (EditText) findViewById(R.id.userText);
		passwordText = (EditText) findViewById(R.id.passwordText);
		rememberCheckBox = (CheckBox) findViewById(R.id.rememberCheckBox);
		accessButton = (Button) findViewById(R.id.accessButton);
		returnTextView = (TextView) findViewById(R.id.returnTextView);

		// listener
		accessButton.setOnClickListener((OnClickListener) this);

		SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
		String username = pref.getString(PREF_USERNAME, "");
		String passwordshare = pref.getString(PREF_PASSWORD, "");
		String checked = pref.getString(PREF_CHECKED, "FALSE");
		
		userText.setText(username);
		passwordText.setText(passwordshare);
		rememberCheckBox.setChecked(Boolean.parseBoolean(checked));
	}

	public enum ActionsEnum {
		authenticate, error;
	}

	@Override
	public void onClick(View v) {
		if (rememberCheckBox.isChecked()) {
			getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
					.edit()
					.putString(PREF_USERNAME, userText.getText().toString())
					.putString(PREF_PASSWORD, passwordText.getText().toString())
					.putString(PREF_CHECKED, "TRUE").commit();
		} else if (!rememberCheckBox.isChecked()) {
			getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit().clear()
					.commit();
		}
		try {
			CustomHttpClient client = new CustomHttpClient(this);
			String action = ActionsEnum.authenticate.toString();
			String paramName1 = "username";
			String paramValue1 = userText.getText().toString();
			String paramName2 = "password";
			String paramValue2 = passwordText.getText().toString();
			returnTextView.setText("");
			userText.setEnabled(false);
			passwordText.setEnabled(false);
			rememberCheckBox.setEnabled(false);
			accessButton.setEnabled(false);
			client.execute(SERVER_URL, action, paramName1,
					paramValue1, paramName2, paramValue2);
		} catch (Exception e) {
			returnTextView.setText(e.toString());
		}
	}

	@Override
	public void executeReturn(String returnValue) {
		String[] returnArray;
		returnArray = returnValue.split("##");

		ActionsEnum action;

		try {
			action = ActionsEnum.valueOf(returnArray[0]);
		} catch (Exception e) {
			action = ActionsEnum.error;
		}

		switch (action) {
		case authenticate:
			authenticateReturn(returnArray);
			break;
		case error:
			errorReturn(returnArray);
			break;
		}
	}

	public void authenticateReturn(String[] returnArray) {
		if (returnArray.length <= 1) {
			userText.setEnabled(true);
			passwordText.setEnabled(true);
			rememberCheckBox.setEnabled(true);
			accessButton.setEnabled(true);
			returnTextView.setText(R.string.invalid_user_or_password);
		} else {
			user = new User(Integer.parseInt(returnArray[2]),
					returnArray[4].toString(), returnArray[6].toString(),
					returnArray[8].toString(), returnArray[10].toString());
			Intent intent = new Intent(LoginActivity.this, MainActivity.class);
			intent.putExtra("user", user);
			LoginActivity.this.startActivity(intent);
			LoginActivity.this.finish();
		}
	}

	public void errorReturn(String[] returnArray) {
		userText.setEnabled(true);
		passwordText.setEnabled(true);
		rememberCheckBox.setEnabled(true);
		accessButton.setEnabled(true);
		returnTextView.setText(R.string.login_error);
	}
}
